#include "LCD.h"
#include <wx/dcclient.h>
#include <wx/bitmap.h>
#include "icon/digit0.xpm"
#include "icon/digit1.xpm"
#include "icon/digit2.xpm"
#include "icon/digit3.xpm"
#include "icon/digit4.xpm"
#include "icon/digit5.xpm"
#include "icon/digit6.xpm"
#include "icon/digit7.xpm"
#include "icon/digit8.xpm"
#include "icon/digit9.xpm"

const int LCD::digit_H = 23;
const int LCD::digit_W = 13;
const int LCD::timerId = wxNewId();

BEGIN_EVENT_TABLE(LCD,wxPanel)
    //(*EventTable(LCD)
    EVT_PAINT(LCD::OnPaint)
    EVT_TIMER(timerId,LCD::OnTimer)
    //*)
END_EVENT_TABLE()

void LCD::OnPaint(wxPaintEvent &event) {
    static const wxBitmap digit[10] = {{digit0},{digit1},{digit2},{digit3},{digit4},{digit5},{digit6},{digit7},{digit8},{digit9}};

    wxPaintDC dc(this);
    if (num >= 1000) {
        SetSize(digit_W*4,digit_H);
    } else {
        SetSize(digit_W*3,digit_H);
    }
    char buf[5];
    sprintf(buf, "%03d", static_cast<int>(num));
    for (int i = 0; buf[i]!=0; i++) {
        dc.DrawBitmap(digit[buf[i]-'0'], i*digit_W,0);
    }
}

void LCD::OnTimer(wxTimerEvent &event) {
    num++;
    Refresh();
}

void LCD::setNum(size_t n) {
    num = n;
    Refresh();
}
